<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="名称" prop="name">
				<el-input v-model="dataForm.name" placeholder="名称"></el-input>
			</el-form-item>
			<el-form-item label="物品类型" prop="itemType">
				<fast-select v-model="dataForm.itemType" dict-type="cardworld_item_type" placeholder="物品类型"></fast-select>
			</el-form-item>
			<el-form-item label="品牌ID" prop="brandId">
				<el-input v-model="dataForm.brandId" placeholder="品牌ID"></el-input>
			</el-form-item>
			<el-form-item label="物品型号" prop="itemNumber">
				<el-input v-model="dataForm.itemNumber" placeholder="物品型号"></el-input>
			</el-form-item>
			<el-form-item label="地理位置" prop="location">
				<el-input v-model="dataForm.location" placeholder="地理位置"></el-input>
			</el-form-item>
			<el-form-item label="标签" prop="tags">
				<!-- <el-input v-model="dataForm.tags" placeholder="标签"></el-input> -->
				<!-- <TagControl :tags="dataForm.tags" @updateTags="(t) => dataForm.tags = t"></TagControl> -->
				<TagControl :tags="dataForm.tags"></TagControl>
			</el-form-item>
			<el-form-item label="属性值" prop="attributes">
				<!-- <el-input v-model="dataForm.attributes" placeholder="属性值，不同类型有不同属性"></el-input> -->
				<!-- <DictControl :dict="dataForm.attributes" @updateDict="(d) => dataForm.attributes = d"></DictControl> -->
				<DictsControl :attributes="dataForm.attributes" :tags="attributesRef"
					@updateTags="(d) => dataForm.attributes = d"></DictsControl>
			</el-form-item>
			<el-form-item label="用户ID" prop="userId">
				<el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useItemsApi, useItemsSubmitApi } from '@/api/cardworld/items'
import TagControl from '@/components/wawa-comp/TagControl.vue'
import DictsControl from '@/components/wawa-comp/DictsControl.vue'



const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const attributesRef = reactive([
	{ name: '说明书链接', placeholder:'输入url', key: 'manualUrl', value: '' },
	{ name: '有效期', placeholder:'输入时间格式', key: 'expiryDate', value: '' },
	{ name: '购买时间', placeholder:'输入时间格式', key: 'buyDate', value: '' },
	{ name: '购买平台', placeholder:'输入字符串', key: 'buyPlatform', value: '' },
])

const dataForm = reactive({
	id: '',
	name: '',
	itemType: '',
	brandId: '',
	itemNumber: '',
	location: '',
	tags: [],
	attributes: {},
	userId: '666666'
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		console.log('重置表单数据')
		dataFormRef.value.resetFields()

		// 重置属性的value值
		attributesRef.forEach(item => {
			item.value = ''
		})
	}

	if (id) {
		getItems(id)
	} else {

		// if (dataForm.attributes.length === 0) {
		// 	dataForm.attributes.push({ name: '说明书链接', key: 'manualUrl', value: 'http://xxxx.com/xx' });
		// 	dataForm.attributes.push({ name: '有效期', key: 'expiryDate', value: '2023-10-01' });
		// }
	}
}

const getItems = (id: number) => {
	useItemsApi(id).then(res => {

		let data = res.data;
		// data.tags = JSON.parse(res.data.tags);
		// let ar = JSON.parse(res.data.attributes);
		// data.attributes=[];
		// data.attributes = ar;
		// // data.attributes.push(JSON.parse(res.data.attributes));
		// console.log(ar)
		console.log(data)
		Object.assign(dataForm, data)
		console.log(dataForm)
	})
}

const dataRules = ref({
	name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useItemsSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
